package tech.xiaozai.common.restful;


import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.experimental.Accessors;


/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-08-30 16:16
 */
@Data
@Accessors(chain = true)
public class RestResp<D> {



    /**
     *  状态码: true代表成功,false代表失败
     */
    private Boolean state;
    /**
     *  时间戮
     */
    private Long timestamp;

    /**
     *  错误信息
     */

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Error error;

    /**
     *  数据
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private D data;

    private RestResp(){ }

    public static<T> RestResp<T> ok(T data) {
        return new RestResp<T>().setData(data).setState(true).setTimestamp(System.currentTimeMillis());

    }

    public static<T> RestResp<T> ok() {
        return new RestResp<T>().setState(true).setTimestamp(System.currentTimeMillis());

    }


    public static RestResp error(Error error){
        return new RestResp().setState(false).setError(error).setTimestamp(System.currentTimeMillis());
    }


}
